import copy
from panda3d.core import Vec2,Point3,Vec3,Vec4
from menu_button import MenuButton
from dialog import Dialog
'''
	Andrew Oberlin
	Date: March 2, 2012
'''
class Hud:
	'''
		Constructor for the Hud to be shown during the game
	'''
	def __init__(self, parent, singleplayer, originalSCV):
		self.parent = parent
		self.count = 0
		self.buttonDict = {
			'move' : "models/menu/hud/move",
			'stop' : "models/menu/hud/stop",
			'create_scv' : "models/menu/hud/create_scv",
			'cancel' : "models/menu/hud/cancel",
			'attack' : "models/menu/hud/attack",
			'building' : "models/menu/hud/building",
			'command_center' : "models/menu/hud/command_center",
			'barracks' : "models/menu/hud/barracks"
		}
		self.buttons = []
		self.singleplayer = singleplayer
		self.loadModels(originalSCV)
		self.parent.accept("aspectRatioChanged", self.__handleAspectChange)
		self.dialog = None
		
	'''
		Loads the menu icons and creates the buttons and their events
	'''
	def loadModels(self, originalSCV):
		self.background = self.parent.loader.loadModelCopy("models/plane")
		self.backgroundTexture = self.parent.loader.loadTexture("models/menu/hud/hud_background.png")
		self.background.setTexture(self.backgroundTexture)
		self.background.setScale(2.67, 1, 0.67)
		self.background.setZ(-0.67)
		self.background.setTransparency(1)
		self.background.reparentTo(self.parent.base.aspect2d)
		
		self.rightMenuDummy = self.parent.base.aspect2d.attachNewNode('singleplayer_menu')
		self.rightMenuDummy.setPos(0.85, 0, -0.60)
		self.createMenu(['move', 'stop'], [
			(self.singleplayer.togglePositionSelection, []),
			(originalSCV.stop, [])
		])
		
		self.count += 1
		self.menuButton = MenuButton(self.parent, 'menu_' + str(self.count), "models/menu/hud/menu_dialog", 
			self.showMenuDialog, Vec3(0.575, 0, -0.6), isMovie=False, scale=Vec3(0.25, 0.25, .075), text="Menu", 
			textPos=Vec3(-0.095,0,-0.01), textScale=Vec3(0.065,0.10,0.040))
		
	'''
		Creates a menu with the given buttons
		
		Params: buttons -- a list of buttons to add (names)
			actions -- a list of actions to bind to the buttons
	'''
	def createMenu(self, buttons, actions):
		row = 0
		self.destroyButtons()
		
		self.buttons = []
		curRow = -1
		
		hGap = 0.20
		vGap = -0.15
		
		for (count, buttonName) in enumerate(buttons):
			if (count % 3 == 0):
				curRow += 1
			if (buttonName != ''):
				newButton = self.getButton(buttonName, actions[count][0], 
					Vec3((count%3)*hGap, 0, curRow*vGap), extraArgs=actions[count][1])
				newButton.reparentTo(self.rightMenuDummy)
				self.buttons.append(newButton)
			
	'''
		Destroys the current buttons array	
	'''
	def destroyButtons(self):
		for button in self.buttons:
			button.removeNode()
			
	
	'''
		Gets the correct type of buttons based on the
		name of the button
	'''
	def getButton(self, key, action, pos, extraArgs=None):
		texture = self.buttonDict[key]
		self.count += 1
		button = MenuButton(self.parent, key + str(self.count), texture, action, pos, 
			isMovie=False, scale=0.15, extraArgs=extraArgs)
		return button
	
	'''
		Resizes the title screen to fit the screen
		Also maintains the aspect ratio
		
		Does not work with fullscreen
	'''
	def __handleAspectChange(self):
		props = self.parent.base.win.getProperties()
		curAspect = props.getXSize()*1.0/props.getYSize()
		if (curAspect != self.parent.WIN_WIDTH/self.parent.WIN_HEIGHT):
			curAspect = self.parent.WIN_WIDTH/self.parent.WIN_HEIGHT
			tmp = copy.deepcopy(props)
			tmp.setSize(props.getXSize(), props.getXSize() / curAspect)
			self.parent.base.win.requestProperties(tmp)
		newScale = 2 * curAspect
		self.scale = Vec3(newScale, 1, 0.67)
		self.background.setScale(self.scale)
		
	'''
		Shows the Menu Dialog
	'''
	def showMenuDialog(self):
		if (self.singleplayer.infoDialog):
			self.singleplayer.infoDialog.hide()
		
		if (not self.dialog):
			self.dialog = Dialog(self.parent, 'menu_dialog')
			self.dialog.addButton(4, "End Game", self.returnToMainMenu)
			self.dialog.addButton(6, "Resume Game", self.dialog.hide)
		else:
			self.dialog.show()
			
	'''
		Returns the game to the main menu
	'''
	def returnToMainMenu(self):
		self.singleplayer.destroy()
		self.destroy()
		self.parent.mainMenu.show()
		
	'''
		Destroys the Hud
	'''
	def destroy(self):
		self.background.removeNode()
		self.rightMenuDummy.removeNode()
		self.menuButton.removeNode()
		if (self.dialog):
			self.dialog.removeNode()